package c03.thread.api;

/*
1、线程优先级会提示(hint)调度器优先调度该线程,但它仅仅是一个提示,调度器可以忽略它
2、如果 cpu 比较忙,那么优先级高的线程会获得更多的时间片,但 cpu 闲时,优先级几乎没作用
 */
public class SetPriorityTest {
    public static void main(String[] args) {
        Runnable task1 = () -> {
            for (int i = 0; i < 1000; i++) {
                if (i % 10 == 0) {
                    System.out.println("-> task1: " + i);
                    Thread.yield();
                }
            }
        };

        Runnable task2 = () -> {
            for (int i = 0; i < 1000; i++) {
                if (i % 10 == 0) {
                    System.out.println("          -> task2: " + i);
                }
            }
        };

        Thread t1 = new Thread(task1, "t1");
        Thread t2 = new Thread(task2, "t2");
        t1.setPriority(Thread.MIN_PRIORITY);
        t2.setPriority(Thread.MAX_PRIORITY);
        t1.start();
        t2.start();
    }
}
